package com.spring.demo.webflux.demo.filter;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.HandlerFilterFunction;
import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

/**
 * HandlerFilterFunction 主要针对基于路由器的端点配置过滤  CityRouter
 * @author wangzhilong
 * @date 2020/7/15
 */
@Component
public class ExampleHandlerFilterFunction implements HandlerFilterFunction {

    /**
     * http://localhost:8080/hello/{name}
     * http://localhost:8080/hello/test
     * 路径中 name== test时返回服务器403状态码
     * @param request
     * @param next
     * @return
     */
    @Override
    public Mono filter(ServerRequest request, HandlerFunction next) {
        if (request.pathVariable("name").equalsIgnoreCase("test")) {
            return ServerResponse.status(HttpStatus.FORBIDDEN).build();
        }
        return next.handle(request);
    }
}
